package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Consuming;
/***
 * 物品领用
 * @author yy
 *
 */
@Service
public class ConsumingDAO extends BaseDAO<Consuming, Integer>{
	
	

	/**
	 * 根据oc 获取领用信息
	 * @param oc	机构编号
	 * @param pb	分页信息
	 * @param map	查询条件
	 */
	public RESULT findByMap(List<Integer> ocs, PageBean pb, Map<String,String> map) {
		if (null == ocs || ocs.size() == 0) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer(" FROM Consuming con WHERE con.deleted = 0 AND con.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if (map != null) {
			String cn = map.get(Parm.CN);
			String name = map.get(Parm.NAME);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND con.deleted = ?");
			}
			if (isNotNull(name)) {
				list.add("%" + name + "%");
				hql.append(" AND con.cname LIKE ?");
			}
			if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND con.cn LIKE ?");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND con.ct >= ?");
			}
			if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND con.ct <= ?");
			}
		}
		hql.append(" ORDER BY con.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	
	/***
	 * 根据cc 获取领用信息
	 * @param cc  领用人
	 * @param pageBean
	 * @param map
	 * @return
	 */
	public RESULT finfBycc(Integer cc,PageBean pageBean, Map<String,String> map){
		if(null == cc) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer("From Consuming con WHERE con.deleted = 0");
		List<Object> list = new ArrayList<Object>();
		hql.append(" AND con.cc = ?");
		list.add(cc);
		if(map != null){
			String cn = map.get(Parm.CN);
			String ct = map.get(Parm.CT);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND con.deleted = ?");
			}if(isNotNull(cn)){
				list.add("%" + cn + "%");
				hql.append(" AND con.cn LIKE ?");
			}if(isNotNull(ct)){
				list.add(DateUtils.getDateByStr(ct, "yyyy-MM-dd"));
				hql.append("AND con.ct >= ?");
			}
		}
		hql.append(" ORDER BY con.code DESC");
		return findToPageBean(hql.toString(),pageBean, list);
	}
	/***
	 * 根据oc 查询领用表里类别为固定资产物品
	 * @param oc
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMapAndCcAndSort(Map<String, String> map, PageBean pb, Integer cc) {
		if (null == cc) return null;
		
		List<Object> list = new ArrayList<Object>();
		List<Object> countParm = new ArrayList<Object>();
		
		StringBuffer sql = new StringBuffer("SELECT DISTINCT g.code,g.name FROM Consuming con, ConsumingItem item, Goods g WHERE con.code = item.coc AND g.code = item.gc AND g.sort = 2");
		StringBuffer countsql = new StringBuffer("SELECT COUNT(DISTINCT item.gc) FROM Consuming con, ConsumingItem item, Goods g WHERE con.code = item.coc AND g.code = item.gc AND g.sort = 2");
		
		countsql.append(" AND con.cc = ?");
		sql.append(" AND con.cc = ?");
		countParm.add(cc);
		list.add(cc);
		
		if (map != null) {
			String gn = map.get(Parm.GN);
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			if(isNotNull(gn)){
				sql.append(" AND g.name LIKE ?");
				countsql.append(" AND g.name LIKE ?");
				list.add("%" + gn + "%");
				countParm.add("%" + gn + "%");
			}if (isNotNull(deleted)) {
				sql.append(" AND item.deleted = ?");
				countsql.append(" AND item.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
				countParm.add(Boolean.parseBoolean(deleted));
			}if (isNotNull(time1)) {
				sql.append(" AND con.ct >= ?");
				countsql.append(" AND con.ct >= ?");
				list.add(DateUtils.getDateByStr(time1, null));
				countParm.add(DateUtils.getDateByStr(time1, null));
			}if (isNotNull(time2)) {
				sql.append(" AND con.ct <= ?");
				countsql.append(" AND con.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, null));
				countParm.add(DateUtils.getDateByStr(time2, null));
			}
		}
		sql.append(" ORDER BY con.code DESC");
		
		return queryToPageBean(pb, sql.toString(), list, countsql.toString(), countParm);
	}
}
